import React from 'react';
import { connect } from 'react-redux';
import store from './store'
import { rechargeWithWeixin } from './store/actionMoney'

function requestWeixinPay(count, callback) {
  // 扫描微信二维码充值，需要微信服务器返回成功之后再修改store的数据
  setTimeout(() => callback({ success: true, result: { count } }), 3000);
}


class Fast extends React.Component {
  // recharge = (count) =>{
  //   store.dispatch({
  //     type: 'recharge',
  //     payload: { count },
  //   })
  // }
  render() {
    // const state = store.getState() // 获取状态管理器中的状态

    return (
      <div>
        <div>我是抢购页面</div>
        <div>我的余额{this.props.money}</div>
        <div><button onClick={() => this.props.rechargeWeixin(300)}>真实充值</button></div>
        <div><button onClick={() => this.props.recharge(111)}>充值</button></div>
      </div>
    );
  }
}

const mapStateToProps = (state) => {
  return { money: state.money.money }
}
const mapDispatchToProps = (dispatch) => {
  return {
    recharge: (count) => dispatch({ type: 'recharge', payload: { count } }),
    // rechargeWeixin: (count) => {
    //   requestWeixinPay(count, (data) => {
    //     dispatch({
    //       type: 'recharge',
    //       payload: { count: data.result.count },
    //     });
    //   });
    // },
    rechargeWeixin: (count) => dispatch(rechargeWithWeixin(count)),
  }
}
export default connect(mapStateToProps, mapDispatchToProps)(Fast);